package com.ssm.controller.student;

import com.alibaba.fastjson.JSONObject;
import com.ssm.controller.BaseController;
import com.ssm.entity.Course;
import com.ssm.entity.Selectcourse;
import com.ssm.service.CourseService;
import com.ssm.service.SelectCourseService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class CourseStudentController extends BaseController {
    @Resource(name="courseService")
    private CourseService courseService;
    @Resource(name = "selectCourseService")
    private SelectCourseService selectCourseService;

    // 查询
    @RequestMapping(value ="course/query",method = RequestMethod.GET)
    public String courseMageageQuery(HttpSession session, Map<String,Object> map){
        List<Course> course=new ArrayList<>();
        logger.info("检查权限");
        String adminId=checkAdmin(session);
        if (!adminId.equals("studentId")){
            return "redirect:/";
        }
        course=courseService.listAllCourse();
        map.put("course",course);
        for (int i = 0; i < course.size(); i++) {
            logger.info(course.get(i));
        }
        return "student/courseChoess";
    }
    //添加
    /*
    @ResponseBody
    @RequestMapping(value = "course/add",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public String add(HttpSession session, @RequestParam Integer courseid, @RequestParam String coursename){
        logger.info("检查管理员权限");
        String adminId=checkAdmin(session);
        if (!adminId.equals("teacherId")){
            return "redirect:/";
        }
        Course course=new Course();
        course.setCourseid(courseid);
        course.setCoursename(coursename);

        Integer IsSuccuess=courseService.createCourse(course);
        JSONObject object=new JSONObject();
        if (IsSuccuess==1){
            logger.info("添加成功");

            object.put("status","success");
        }else{
            logger.info("添加错误");
            object.put("status","false");
        }
        return object.toJSONString();
    }
    */

    //选课
    @ResponseBody
    @RequestMapping(value = "course/choess",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public String delete(HttpSession session,  @RequestParam Integer id){
        logger.info("检查权限");
        String adminId=checkAdmin(session);
        if (!adminId.equals("studentId")){
            return "redirect:/";
        }
        Object o=session.getAttribute("studentId");
        Selectcourse selectcourse=new Selectcourse();
        selectcourse.setCourseid(id);
        selectcourse.setId(Integer.parseInt(o.toString()));
        Integer IsSuccess=selectCourseService.createSelectCourse(selectcourse);

        JSONObject object=new JSONObject();
        if (1==1){//IsSuccess==1
            logger.info("选课成功");

            object.put("status","success");
        }else{
            logger.info("选课错误");
            object.put("status","false");
        }
        return object.toJSONString();
    }


}
